package com.api.user.controller;
import com.api.common.JSONUtils;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.Coupon;
import com.api.user.model.CouponAdd;
import com.api.user.model.CouponUse;
import com.api.user.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.api.core.annotation.PowerEnable;
import io.swagger.annotations.*;



/**
* Created by wanghuiwen on 2021/04/12.
*/
@PowerEnable(name = "优惠券",url = "/coupon")
@Api(value = "优惠券", tags = {"优惠券"})
@RestController
@RequestMapping("/coupon")
public class CouponController extends Ctrl{
    @Resource
    private CouponService couponService;

    @ApiOperation(value = "优惠券添加", tags = {"优惠券"}, notes = "优惠券添加")
    @PostMapping(value="/add",name="优惠券添加")
    public Result add(@ApiParam CouponAdd coupon) {
        coupon.setState(1);
        coupon.setStartTime(new Date().getTime());
        return couponService.add(coupon);
    }



    @ApiOperation(value = "用户端-优惠券-领取", tags = {"优惠券"}, notes = "用户端-优惠券-领取")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "couponId",required=true, value = "优惠券id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/receive",name="用户端-优惠券-领取")
    public Result receive(@RequestParam Long couponId, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        return couponService.receive(couponId,userId);
    }

    @ApiOperation(value = "用户端-可领取优惠券列表", tags = {"优惠券"}, notes = "用户端-可领取优惠券列表")

    @PostMapping(value="/list/user",name="用户端-可领取优惠券列表")
    public Result listUser(Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        return couponService.listUser(userId);
    }


    @ApiOperation(value = "管理端-优惠券列表", tags = {"陪玩"}, notes = "管理端-优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "管理端-优惠券列表")
    public Result list(
            String title,
            Integer type,
            @RequestParam(defaultValue = "{}") String order,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Authentication authentication) {
//        Long userId = getAuthUser(authentication).getId();
        Map<String, Object> params = new HashMap<>();
        params.put("title",title);
        params.put("type",type);
        return couponService.list(params, order, page, size);
    }


    @ApiOperation(value = "用户端-我的优惠券", tags = {"优惠券"}, notes = "用户端-我的优惠券")

    @PostMapping(value="/my",name="用户端-可领取优惠券列表")
    public Result my(
            String code,
            @RequestParam(defaultValue = "{}") String order,
            Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        return couponService.my(userId,code,orderParams);
    }


    @ApiOperation(value = "用户端-优惠券使用", tags = {"优惠券"}, notes = "用户端-优惠券使用")
    @PostMapping(value="/apply",name="用户端-优惠券使用")
    public Result apply(@RequestBody CouponUse couponUse, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        return couponService.apply(couponUse);
    }


    @PostMapping(value="/delete",name="用户端-可领取优惠券列表")
    public Result delete(Long id) {
        return couponService.delete(id);
    }
}
